package com.example.demo.service.impl;

import com.example.demo.context.BaseContext;
import com.example.demo.mapper.ForumCommentMapper;
import com.example.demo.mapper.ForumMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserSupportMapper;
import com.example.demo.pojo.dto.CommentAddDTO;
import com.example.demo.pojo.dto.ForumAddDTO;
import com.example.demo.pojo.entity.Forum;
import com.example.demo.pojo.entity.ForumComment;
import com.example.demo.pojo.entity.User;
import com.example.demo.pojo.entity.UserSupport;
import com.example.demo.pojo.vo.ForumDetailVO;
import com.example.demo.pojo.vo.ForumVO;
import com.example.demo.pojo.vo.UserCommentVO;
import com.example.demo.pojo.vo.UserForumVO;
import com.example.demo.service.ForumService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ForumServiceImpl implements ForumService {
    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserSupportMapper userSupportMapper;
    @Autowired
    private ForumCommentMapper forumCommentMapper;

    @Override
    public ForumVO list() {
        Long userId=BaseContext.getCurrentId();
        ForumVO forumVO=new ForumVO();
        List<Forum> forumList=forumMapper.list();
        List<UserSupport> userSupportList=userSupportMapper.list(userId);

        forumVO.setForumList(forumList);
        forumVO.setUserSupportList(userSupportList);
        return forumVO;
    }

    @Override
    public void add(ForumAddDTO forumAddDTO) {
        Forum forum = new Forum();
        BeanUtils.copyProperties(forumAddDTO,forum);
        Long userId= BaseContext.getCurrentId();
        User user=userMapper.getById(userId);
        forum.setName(user.getName());
        forum.setUserId(userId);
        forum.setSupport(0);
        forum.setCreateTime(LocalDateTime.now());
        forumMapper.insert(forum);
    }

    @Override
    @Transactional
    public void updateSupport(Long id, Integer support) {
        Long userId=BaseContext.getCurrentId();
        UserSupport userSupport=userSupportMapper.getByUserIdAndForumId(userId,id);
        if(userSupport!=null){
            userSupportMapper.deleteById(userSupport.getId());
        }else{
            userSupport=new UserSupport();
            userSupport.setForumId(id);
            userSupport.setUserId(userId);
            userSupport.setCreateTime(LocalDateTime.now());
            userSupportMapper.insert(userSupport);
        }
        forumMapper.updateSupport(id,support);
    }

    @Override
    public ForumDetailVO getDetail(Long forumId) {
        ForumDetailVO forumDetailVO=new ForumDetailVO();

        List<UserCommentVO> userCommentVOList = forumMapper.getUserCommentDetail(forumId);

        UserForumVO userForumVO=forumMapper.getUserForumDetail(forumId);
        Integer supportNum=userSupportMapper.getUserSupportNumByForumId(forumId);
        Integer commentNum=forumCommentMapper.getCommentNumByForumId(forumId);
        userForumVO.setCommentNum(commentNum);
        userForumVO.setSupportNum(supportNum);

        forumDetailVO.setUserForumVO(userForumVO);
        forumDetailVO.setUserCommentVOList(userCommentVOList);
        return forumDetailVO;
    }

    @Override
    public void addComment(CommentAddDTO commentAddDTO) {
        ForumComment forumComment = new ForumComment();
        forumComment.setUserId(BaseContext.getCurrentId());
        forumComment.setComment(commentAddDTO.getComment());
        forumComment.setForumId(commentAddDTO.getForumId());
        forumComment.setCreateTime(LocalDateTime.now());
        forumCommentMapper.insert(forumComment);
    }
}
